package com.yanjun.messenger

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.View.OnClickListener
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.yanjun.progress.R

class MagicActivity : AppCompatActivity() {

    //
     var mTvTest: TextView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mTvTest = findViewById(R.id.tvTest)
        mTvTest!!.setOnClickListener(OnClickListener {
            val intent = Intent(this, Main2Activity::class.java)
            startActivity(intent)
            val bundle = Bundle()
            bundle.putString("test", "activity1 发送消息到服务")
            MagicMessenger.post("service", bundle)
        })
        MagicMessenger.subscribe("MagicActivity") { data ->
            val test = data.getString("test")
            val text = "MagicActivity 接收到消息$test"
            Toast.makeText(this@MagicActivity, text, Toast.LENGTH_SHORT).show()
            Log.e("MagicActivity ", "onMsgCallBack: $text")
        }
//        val intent = Intent(this, TestService::class.java)
//        startService(intent)
    }


    override fun onDestroy() {
        super.onDestroy()

    }
}